package com.jz.yanshe.ui;

import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.EarningResponse;
import com.jz.jd.rest.response.VIPDetailResponse;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;
import jz.com.feierman.R;


/**
 * Created by Administrator on 2018/3/15.
 */

public class EarningActivity extends BaseActivity {


    @BindView(R.id.tv_shouyi)
    TextView tv_shouyi;

    @BindView(R.id.tv_balance)
    TextView tv_balance;
    @BindView(R.id.tv_date)
    TextView tv_date;

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.tv8)
    TextView tv8;
    @BindView(R.id.tv9)
    TextView tv9;
    @BindView(R.id.tv_yajin)
    TextView tv_yajin;


    VIPDetailResponse.vipDetail vipDetail;

    @Override
    protected void initViews() {
        tv_shouyi.setText(vipDetail.total_earnings);
        tv_balance.setText(String.format("%.2f", Double.valueOf(vipDetail.balance)));
        tv_yajin.setText(String.format("%.2f", Double.valueOf(vipDetail.deposit)));
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        if(month<10){
            tv_date.setText(year + "-0" + month);
        }else{
            tv_date.setText(year + "-" + month);
        }
        getEarning();
    }

    @Override
    protected void initData() {
        vipDetail = (VIPDetailResponse.vipDetail) getIntent().getSerializableExtra("obj");

    }

    @OnClick(R.id.btn_shouyi_detail)
    void btn_shouyi_detail(){
        Bundle bundle = new Bundle();
        bundle.putString("userid",vipDetail.user_id);
        bundle.putString("date",tv_date.getText().toString().trim());
        startActivity(ShouyiDetailActivity.class,bundle);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_earning;
    }

    @OnClick(R.id.btn_datepicker)
    void btn_datepicker() {
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(EarningActivity.this, 0, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear,
                                  int startDayOfMonth) {
                String textString = String.format("选择年月：%d-%d\n", startYear,
                        startMonthOfYear + 1);
                if(startMonthOfYear + 1<10){
                    tv_date.setText(startYear + "-0" +  (startMonthOfYear + 1));
                }else{
                    tv_date.setText(startYear + "-" +  (startMonthOfYear + 1));
                }
//                tv_date.setText(startYear + "-" + (startMonthOfYear + 1));
                getEarning();
            }
        }, Integer.valueOf(tv_date.getText().toString().trim().split("-")[0]), Integer.valueOf(tv_date.getText().toString().trim().split("-")[1]) - 1, c.get(Calendar.DATE)).show();
    }


    private void getEarning() {
        System.out.println(tv_date.getText().toString().trim()+"    aaaaaaaaa");
        RestProxy.getInstance().getEarning(PreferencesHelper.getInstance().getString(this, "token"), tv_date.getText().toString().trim(), new Observer<EarningResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(EarningResponse earningResponse) {
//                JSONArray array = null;
//                try {
//                    array = new JSONArray(earningResponse.list);
//                    array.optString(0);
//                    System.out.println(array.optString(0));
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
                tv1.setText(String.format("%.2f", Double.valueOf(0)));
                tv2.setText(String.format("%.2f", Double.valueOf(0)));
                tv3.setText(String.format("%.2f", Double.valueOf(0)));
                tv4.setText(String.format("%.2f", Double.valueOf(0)));
                tv5.setText(String.format("%.2f", Double.valueOf(0)));
                tv6.setText(String.format("%.2f", Double.valueOf(0)));
                tv7.setText(String.format("%.2f", Double.valueOf(0)));
                tv8.setText(String.format("%.2f", Double.valueOf(0)));
                tv9.setText(String.format("%.2f", Double.valueOf(0)));

                for (List<String> list : earningResponse.list) {
                    if (list.get(0).equals("1")) {
                        tv4.setText(String.format("%.2f", Double.valueOf(list.get(3))));
                        tv5.setText(String.format("%.2f", Double.valueOf(list.get(2))));
                        tv6.setText(String.format("%.2f", Double.valueOf(list.get(1))));
                    }
                    if (list.get(0) == "2") {
                        tv1.setText(String.format("%.2f", Double.valueOf(list.get(3))));
                        tv2.setText(String.format("%.2f", Double.valueOf(list.get(2))));
                        tv3.setText(String.format("%.2f", Double.valueOf(list.get(1))));
                    }
                    if (list.get(0) == "3") {
                        tv7.setText(String.format("%.2f", Double.valueOf(list.get(3))));
                        tv8.setText(String.format("%.2f", Double.valueOf(list.get(2))));
                        tv9.setText(String.format("%.2f", Double.valueOf(list.get(1))));
                    }
                }
//                tv1.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(1).get(3))));
//                tv2.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(1).get(2))));
//                tv3.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(1).get(1))));
//
//                tv7.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(2).get(3))));
//                tv8.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(2).get(2))));
//                tv9.setText(String.format("%.2f",Double.valueOf(earningResponse.list.get(2).get(1))));}

            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    if (((BusinessException) e).errorCode == 0) {
                        tv1.setText(String.format("%.2f", Double.valueOf(0)));
                        tv2.setText(String.format("%.2f", Double.valueOf(0)));
                        tv3.setText(String.format("%.2f", Double.valueOf(0)));
                        tv4.setText(String.format("%.2f", Double.valueOf(0)));
                        tv5.setText(String.format("%.2f", Double.valueOf(0)));
                        tv6.setText(String.format("%.2f", Double.valueOf(0)));
                        tv7.setText(String.format("%.2f", Double.valueOf(0)));
                        tv8.setText(String.format("%.2f", Double.valueOf(0)));
                        tv9.setText(String.format("%.2f", Double.valueOf(0)));
                    }
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }


}


